﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using LibVLCSharp.Shared;
using LibVLCSharp.WinForms;

namespace RhythmGame
{
    internal class BackgroundLoader
    {
        internal static Panel startInterface;
        internal static Panel endInterface;

        internal static Button btnResume;
        internal static Button btnRestart;
        internal static Button btnExit;

        // VLC相关静态字段，便于后续控制
        public static LibVLC libVLC;
        public static MediaPlayer audioPlayer;
        public static MediaPlayer videoPlayer;
        public static VideoView videoView;

        internal static void LoadBackground(Game game, BeatMap map)
        {
            // 初始化LibVLC
            if (libVLC == null)
            {
                Core.Initialize();
                libVLC = new LibVLC();
            }

            // 音频播放
            string audioPath = Path.Combine("Songs", map.Path, map.AudioPath);
            if (!File.Exists(audioPath))
            {
                MessageBox.Show("音频文件不存在: " + audioPath);
                return;
            }
            audioPlayer = new MediaPlayer(libVLC);
            var audioMedia = new Media(libVLC, audioPath);
            audioPlayer.Media = audioMedia;
            audioPlayer.Volume = Settings.Instance.Volume;
            audioPlayer.Play();

            // 视频播放
            if (game.playVideo)
            {
                // 创建VideoView控件
                videoView = new VideoView
                {
                    MediaPlayer = null,
                    Size = new Size(Settings.Instance.WinWidth, Settings.Instance.WinHeight),
                    Location = new Point(0, 0),
                    BackColor = Color.Black
                };
                game.Controls.Add(videoView);
                videoView.SendToBack();

                string videoPath = Path.Combine("Songs", map.Path, map.VideoPath);
                if (!File.Exists(videoPath))
                {
                    MessageBox.Show("视频文件不存在: " + videoPath);
                    return;
                }
                // 创建MediaPlayer并绑定到VideoView
                videoPlayer = new MediaPlayer(libVLC);
                videoView.MediaPlayer = videoPlayer;
                var videoMedia = new Media(libVLC, videoPath);
                videoPlayer.Media = videoMedia;
                videoPlayer.Volume = 0; // 视频静音

                // 亮度调节（0.0-2.0，1.0为默认）
                videoPlayer.SetAdjustInt(LibVLCSharp.Shared.VideoAdjustOption.Enable, 1);
                float brightness = Math.Max(0.0f, Math.Min(2.0f, Settings.Instance.VideoBrightness / 100.0f));
                videoPlayer.SetAdjustFloat(LibVLCSharp.Shared.VideoAdjustOption.Brightness, brightness);

                videoPlayer.Play();
            }

            btnResume = new Button
            {
                Text = "继续",
                Font = Settings.Instance.ButtonTextFont,
                ForeColor = Color.White,
                Size = new Size(Settings.Instance.WinWidth / 5, Settings.Instance.WinHeight / 10),
                Location = new Point(Settings.Instance.WinWidth / 2 - Settings.Instance.WinWidth / 10,
                    Settings.Instance.WinHeight / 2 - Settings.Instance.WinHeight / 20 - Settings.Instance.WinHeight / 5),
                Visible = false
            };
            btnResume.Click += (s, e) => game.TogglePause();

            btnRestart = new Button
            {
                Text = "重新开始",
                Font = Settings.Instance.ButtonTextFont,
                ForeColor = Color.White,
                Size = new Size(Settings.Instance.WinWidth / 5, Settings.Instance.WinHeight / 10),
                Location = new Point(Settings.Instance.WinWidth / 2 - Settings.Instance.WinWidth / 10,
                    Settings.Instance.WinHeight / 2 - Settings.Instance.WinHeight / 20),
                Visible = false
            };
            btnRestart.Click += (s, e) => game.RestartGame();

            btnExit = new Button
            {
                Text = "退出",
                Font = Settings.Instance.ButtonTextFont,
                ForeColor = Color.White,
                Size = new Size(Settings.Instance.WinWidth / 5, Settings.Instance.WinHeight / 10),
                Location = new Point(Settings.Instance.WinWidth / 2 - Settings.Instance.WinWidth / 10,
                    Settings.Instance.WinHeight / 2 - Settings.Instance.WinHeight / 20 + Settings.Instance.WinHeight / 5),
                Visible = false
            };
            btnExit.Click += (s, e) => Application.Exit();

            game.Controls.Add(btnResume);
            game.Controls.Add(btnRestart);
            game.Controls.Add(btnExit);

            btnExit.BringToFront();
            btnRestart.BringToFront();
            btnResume.BringToFront();
        }

        internal static void LoadStartInterface(Game game, BeatMap map)
        {
            startInterface = new Panel
            {
                Size = new Size(Settings.Instance.WinWidth, Settings.Instance.WinHeight),
                Location = new Point(0, 0),
                BackColor = Color.Black,
                Visible = false
            };
            Label songName = new Label
            {
                Text = "歌曲：" + map.Name,
                Font = Settings.Instance.TextFont,
                ForeColor = Color.White,
                Size = new Size(Settings.Instance.WinWidth, Settings.Instance.WinHeight / 10),
                Location = new Point(0, Settings.Instance.WinHeight / 10),
                TextAlign = ContentAlignment.MiddleCenter
            };
            Label songArtist = new Label
            {
                Text = "歌手：" + map.Artist,
                Font = Settings.Instance.TextFont,
                ForeColor = Color.White,
                Size = new Size(Settings.Instance.WinWidth, Settings.Instance.WinHeight / 10),
                Location = new Point(0, Settings.Instance.WinHeight / 10 * 2),
                TextAlign = ContentAlignment.MiddleCenter
            };
            Label songMapper = new Label
            {
                Text = "谱师：" + map.Mapper,
                Font = Settings.Instance.TextFont,
                ForeColor = Color.White,
                Size = new Size(Settings.Instance.WinWidth, Settings.Instance.WinHeight / 10),
                Location = new Point(0, Settings.Instance.WinHeight / 10 * 3),
                TextAlign = ContentAlignment.MiddleCenter
            };
            Label duringTime = new Label
            {
                Text = "时长：" + map.Time,
                Font = Settings.Instance.TextFont,
                ForeColor = Color.White,
                Size = new Size(Settings.Instance.WinWidth, Settings.Instance.WinHeight / 10),
                Location = new Point(0, Settings.Instance.WinHeight / 10 * 4),
                TextAlign = ContentAlignment.MiddleCenter
            };
            Label songDifficulty = new Label
            {
                Text = "难度：" + map.Difficulty,
                Font = Settings.Instance.TextFont,
                ForeColor = Color.White,
                Size = new Size(Settings.Instance.WinWidth, Settings.Instance.WinHeight / 10),
                Location = new Point(0, Settings.Instance.WinHeight / 10 * 5),
                TextAlign = ContentAlignment.MiddleCenter
            };
            Label songLevel = new Label
            {
                Text = "等级：" + map.Level,
                Font = Settings.Instance.TextFont,
                ForeColor = Color.White,
                Size = new Size(Settings.Instance.WinWidth, Settings.Instance.WinHeight / 10),
                Location = new Point(0, Settings.Instance.WinHeight / 10 * 6),
                TextAlign = ContentAlignment.MiddleCenter
            };
            Label songTotalNotes = new Label
            {
                Text = "总音符数：" + map.GetTotalNotes(),
                Font = Settings.Instance.TextFont,
                ForeColor = Color.White,
                Size = new Size(Settings.Instance.WinWidth, Settings.Instance.WinHeight / 10),
                Location = new Point(0, Settings.Instance.WinHeight / 10 * 7),
                TextAlign = ContentAlignment.MiddleCenter
            };
            startInterface.Controls.Add(songName);
            startInterface.Controls.Add(songArtist);
            startInterface.Controls.Add(songMapper);
            startInterface.Controls.Add(duringTime);
            startInterface.Controls.Add(songDifficulty);
            startInterface.Controls.Add(songLevel);
            startInterface.Controls.Add(songTotalNotes);
            game.Controls.Add(startInterface);
            startInterface.Visible = true;
            startInterface.BringToFront();
        }
    }
}